/** Created by CUIJA on 05-19-2017.*/

var angular = require('angular');
var coreRoutes = require('./routes/core.routes');
//service
var navMenuService = require('./services/menu.service');
var merchantService = require('./services/merchant.service');

var navbarComponent = require('./components/navbar/navbar');
//controller
var navbarController = require('./components/navbar/navbar.controller');
var homeController = require('./controller/home.controller');
var merchantController = require('./controller/merchant.controller');
var merchantPassedController = require('./controller/merchant.passed.controller');
var merchantInprocessController = require('./controller/merchant.inprocess.controller');
var merchantRejectedController = require('./controller/merchant.rejected.controller');

//interceptors
var loginInterceptor = require('./interceptor/login.interceptor');

const moduleName = 'core';

angular.module(moduleName, [])
    .factory('loginInterceptor',loginInterceptor)
    .factory('navMenuService', navMenuService)
    .factory('merchantService',merchantService) //register Interceptor
    .config([ '$httpProvider', function($httpProvider) {
        $httpProvider.interceptors.push('loginInterceptor');
    }])
    .config(coreRoutes)
    .component('navbar', navbarComponent)

    .controller('navbarController', navbarController)
    .controller('homeController',homeController)
    .controller('merchantController',merchantController)
    .controller('merchantPassedController',merchantPassedController)
    .controller('merchantInprocessController',merchantInprocessController)
    .controller('merchantRejectedController',merchantRejectedController)

    .filter('formateDate',function(){   //date filter
        return function(input){
            var date = new Date(input);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            if(month<10){
                month = '0'+month;
            }
            var day = date.getDate();
            if(day<10){
                day = '0'+day;
            }
            var hours = date.getHours();
            if(hours<10){
                hours = '0'+hours;
            }
            var minus = date.getMinutes();
            if(minus<10){
                minus = '0'+minus;
            }
            var secs = date.getSeconds();
            if(secs<10){
                secs = '0'+secs;
            }
            var dateStr = year+'-'+month+'-'+day+' '+hours+':'+minus+':'+secs;
            return dateStr;
        }
    })
    .filter('formatStatus',function(){  //status filter
        return function(input){
            if(0==input){
                return 'Inprocess';
            }else if(1==input){
                return 'Passed';
            }else {
                return 'Rejected';
            }
        }
    }).filter('formatBan',function(){   //ban filter
    return function(input){
        if(input){
            return 'Baned';
        }else {
            return 'NotBan';
        }
    }
})

    .run(run);

run.$inject = [
    '$location',
    '$rootScope',
    '$http',
    'loginService'
];

function run($location, $rootScope, $http,loginService) {
    localStorage.user = {};
    $rootScope.$on('$locationChangeStart', function (event, current, previous) {
        var sub = current.substring(7);
        var n = sub.indexOf('/');
        sub = sub.substring(n);
        var st = 'routeChangeLog: ';

        console.log(st+ "ui-route change to" + sub);
        if(sub.startsWith('/#!/acm')){
            // $http.get('/O2OAdministrator/api/validate')
            //     .then(function(res){
            //         let rs = res.data;
            //         if(!rs.result){
            //             console.log(st+rs.errMsg);
            //             $location.path('/');
            //             return;
            //         }else {
            //             console.log(st+'have permisstion');
            //             console.log(st+JSON.stringify(rs.data));
            //             $rootScope.user = rs.data;
            //             loginService.setUser(rs.data);
            //         }
            //     });
            var u = sessionStorage.user;
            if(!u){
                $location.path('/');
            }
            if(!(sub=='/#!/acm/merchant/inprocess' || sub=='/#!/acm/merchant/inprocess/')){
                $rootScope.pendingCountStatus = 1;
            }

        }
    });
}

module.exports = moduleName;